/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Interfaces;

import Clases.Licencia;
import Clases.Titular;
import java.awt.Image;
import java.sql.Connection;
import java.util.Date;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JREmptyDataSource;  
import net.sf.jasperreports.engine.JRException;  
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter; 
import net.sf.jasperreports.engine.JasperFillManager;  
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;


public class ImprimirLicencia extends javax.swing.JFrame {

    //Handler para la conexión a la BD.
    
    Connection miConexion;
    
    //Dato ingresado por pantalla.
    
    private Integer documentoTitular; //DNI que se ingresa en pantalla para posterior validación y parsing
    private boolean buscar=false;
    
    
    // Clases donde se cargan los datos obtenidos desde la BD para
    // informar al usuario y validar la licencia que se quiere emitir.
    
    private Titular titularValidado=new Titular();
    private Licencia[] licenciasValidadas={new Licencia(), new Licencia(), new Licencia(), new Licencia(), new Licencia(), new Licencia()};
    
    public ImprimirLicencia() {
        
        //Declaramos dos instancias sin inicializar. 
        //Cargamos un titular con el constructor por defecto.
        //Cargamos seis licencias con el constructor por defecto.
        
        initComponents();
        setLocationRelativeTo(null);
    }
    
    
    //Funciones de interfaz
    private Licencia seleccionarLicencia(String claseSeleccionada){
        for(int i=0;i<6;i++){
            if (licenciasValidadas[i].getTipoLicencia().equals(claseSeleccionada))
                return licenciasValidadas[i];
        }
        return null;
    }
    
    private void fieldFiller(Titular titularObtenido, Licencia licenciaObtenida){ //Llena los campos de la interfaz cuando se presiona "Validar" después de ingresar un DNI.
            
        ImageIcon icono = new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Perfil.jpg"));
        Image imagen = icono.getImage();
        ImageIcon iconoEscalado = new ImageIcon (imagen.getScaledInstance(100,100,Image.SCALE_SMOOTH));
        labelImagen.setIcon(iconoEscalado);

        //Mostramos en los textfields la información traída desde la BD.
        labelTipoDNI.setText(titularObtenido.getTipoDoc());
        labelDNI.setText(Integer.toString(documentoTitular));
        labelNombre.setText(titularObtenido.getNombre());
        labelApellido.setText(titularObtenido.getApellido());
        labelFechaNacimiento.setText((titularObtenido.getFechaNacimiento()).toString());
        labelDomicilio.setText(titularObtenido.getDireccion());
        labelClase.setText(licenciaObtenida.getTipoLicencia());
        labelOtorgamiento.setText(licenciaObtenida.getFechaEmision().toString());
        labelVencimiento.setText(licenciaObtenida.getFechaCaducidad().toString());
        if (titularObtenido.getDonanteOrganos().equals("true"))
            labelDonante.setText("SI");
        else labelDonante.setText("NO");
        labelGrupoSanguineo.setText(titularObtenido.getGrupoSanguineo());
        labelFactor.setText(titularObtenido.getFactorSanguineo());
        labelObservaciones.setText(titularObtenido.getObservaciones());
        labelCodigo.setText("1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7");
        
        ImageIcon icono_codigo = new javax.swing.ImageIcon(getClass().getResource("/Imagenes/codigoBarras.jpg"));
        Image imagen_codigo = icono_codigo.getImage();
        ImageIcon icono_codigoEscalado = new ImageIcon (imagen_codigo.getScaledInstance(340,50,Image.SCALE_SMOOTH));
        labelImagenCodigo.setIcon(icono_codigoEscalado);
    }
    
    public void creaReporte(){ 
        JasperReport reporte_frente, reporte_dorso, comprobante;
        JasperPrint reporte_view_frente, reporte_view_dorso, reporte_view_comprobante;
     
        try {
            //Licencia - frente
            //Localiza los reportes para luego realizar la transferencia de datos
            URL  frente = this.getClass().getResource("/Reportes/Licencia frente.jasper");
            reporte_frente = (JasperReport) JRLoader.loadObject( frente );           
            
            //Setea los parametros obtenidos de la base de datos en un map para luego pasarlos al reporte
            Map parametro_frente = new HashMap();
            parametro_frente.put("tipo_DNI",labelTipoDNI.getText());
            parametro_frente.put("DNI",labelDNI.getText());
            parametro_frente.put("apellido",labelApellido.getText());
            parametro_frente.put("nombre", labelNombre.getText());
            parametro_frente.put("fecha_nac",labelFechaNacimiento.getText());
            parametro_frente.put("domicilio",labelDomicilio.getText());
            parametro_frente.put("otorgamiento",labelOtorgamiento.getText());
            parametro_frente.put("vencimiento",labelVencimiento.getText());
            parametro_frente.put("clase",labelClase.getText());
            parametro_frente.put("categoria",labelCategoria.getText());
            
            String perfil = "/Imagenes/Perfil.jpg";   
            parametro_frente.put("foto", this.getClass().getResourceAsStream(perfil));

            reporte_view_frente = JasperFillManager.fillReport(reporte_frente, parametro_frente, new JREmptyDataSource());
            
            
            //Licencia - dorso
            //Localiza los reportes para luego realizar la transferencia de datos
            URL  dorso = this.getClass().getResource("/Reportes/Licencia dorso.jasper");
            reporte_dorso = (JasperReport) JRLoader.loadObject( dorso );
            
            //Setea los parametros obtenidos de la base de datos en un map para luego pasarlos al reporte
            Map parametro_dorso = new HashMap();
            parametro_dorso.put("observaciones", labelObservaciones.getText());
            parametro_dorso.put("donante", labelDonante.getText());
            parametro_dorso.put("grupo", labelGrupoSanguineo.getText());
            parametro_dorso.put("factor", labelFactor.getText());
            
            String imagenCodigo = "/Imagenes/codigoBarras.jpg";   
            parametro_dorso.put("imagen_codigo", this.getClass().getResourceAsStream(imagenCodigo));
            
            parametro_dorso.put("codigo", labelCodigo.getText());

            reporte_view_dorso= JasperFillManager.fillReport(reporte_dorso, parametro_dorso, new JREmptyDataSource());
            
            
            //Comprobante
            //Localiza los reportes para luego realizar la transferencia de datos
            URL  comp = this.getClass().getResource("/Reportes/Comprobante.jasper");
            comprobante = (JasperReport) JRLoader.loadObject(comp);           
            
            //Setea los parametros obtenidos de la base de datos en un map para luego pasarlos al reporte
            Date now = new Date(System.currentTimeMillis());
            SimpleDateFormat date = new SimpleDateFormat("dd-MM-YYYY");
            
            Map parametro_comprobante = new HashMap();
            parametro_comprobante.put("tipoDocumento",labelTipoDNI.getText());
            parametro_comprobante.put("DNI",labelDNI.getText());
            parametro_comprobante.put("apellido",labelApellido.getText());
            parametro_comprobante.put("nombre", labelNombre.getText());
            parametro_comprobante.put("hoy",date.format(now));
            parametro_comprobante.put("vencimiento",labelVencimiento.getText());
            parametro_comprobante.put("clase",labelClase.getText());
            parametro_comprobante.put("imagen_codigo", this.getClass().getResourceAsStream(imagenCodigo));
            parametro_comprobante.put("codigo", labelCodigo.getText());

            reporte_view_comprobante = JasperFillManager.fillReport(comprobante, parametro_comprobante, new JREmptyDataSource());
            
            
            //Muestra la licencia en pantalla
            JasperViewer.viewReport(reporte_view_frente,false);
            JasperViewer.viewReport(reporte_view_dorso,false);
            JasperViewer.viewReport(reporte_view_comprobante,false);
            
            
            //Guarda la licencia en formato Html
            JRExporter exporter_frente = new JRHtmlExporter();
            exporter_frente.setParameter(JRExporterParameter.JASPER_PRINT,reporte_view_frente); 
            exporter_frente.setParameter(JRExporterParameter.OUTPUT_FILE,new java.io.File("Licencias/"+(labelDNI.getText())+"-"+(labelOtorgamiento.getText())+"-"+(labelClase.getText())+"-reporteFrente.html"));
            exporter_frente.exportReport();
            
            JRExporter exporter_dorso = new JRHtmlExporter();
            exporter_dorso.setParameter(JRExporterParameter.JASPER_PRINT,reporte_view_dorso); 
            exporter_dorso.setParameter(JRExporterParameter.OUTPUT_FILE,new java.io.File("Licencias/"+(labelDNI.getText())+"-"+(labelOtorgamiento.getText())+"-"+(labelClase.getText())+"-reporteDorso.html"));
            exporter_dorso.exportReport();
            
            JRExporter exporter_comprobante = new JRHtmlExporter();
            exporter_comprobante.setParameter(JRExporterParameter.JASPER_PRINT,reporte_view_comprobante); 
            exporter_comprobante.setParameter(JRExporterParameter.OUTPUT_FILE,new java.io.File("Licencias/"+(labelDNI.getText())+"-"+(labelOtorgamiento.getText())+"-"+(labelClase.getText())+"-comprobante.html"));
            exporter_comprobante.exportReport();
            
            JOptionPane.showMessageDialog(null, "Reporte ceado.");
            
        } catch (JRException ex) {
            Logger.getLogger(ImprimirLicencia.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "No pudo imprimirse la licencia.");
        }
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel6 = new javax.swing.JPanel();
        jLabel21 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel30 = new javax.swing.JLabel();
        textFieldDocumentoTitular = new javax.swing.JTextField();
        bBuscar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        bVistaPrevia = new javax.swing.JButton();
        comboBoxClase = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        labelTipoDNI = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        labelDNI = new javax.swing.JLabel();
        labelApellido = new javax.swing.JLabel();
        labelNombre = new javax.swing.JLabel();
        labelFechaNacimiento = new javax.swing.JLabel();
        labelDomicilio = new javax.swing.JLabel();
        labelOtorgamiento = new javax.swing.JLabel();
        labelVencimiento = new javax.swing.JLabel();
        labelClase = new javax.swing.JLabel();
        labelCategoria = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        labelImagen = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel4 = new javax.swing.JPanel();
        jLabel22 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        labelDonante = new javax.swing.JLabel();
        labelGrupoSanguineo = new javax.swing.JLabel();
        labelObservaciones = new javax.swing.JLabel();
        labelFactor = new javax.swing.JLabel();
        labelCodigo = new javax.swing.JLabel();
        labelImagenCodigo = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        bCancelar = new javax.swing.JButton();
        bImprimir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Imprimir Licencia");
        setResizable(false);

        jPanel6.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel21.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        jLabel21.setText("Imprimir licencia");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jLabel21)
                .addContainerGap(34, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel21)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Ingresar"));
        jPanel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel30.setText("DNI:");

        textFieldDocumentoTitular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textFieldDocumentoTitularActionPerformed(evt);
            }
        });

        bBuscar.setText("Buscar");
        bBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarActionPerformed(evt);
            }
        });

        jLabel1.setText("CLASE:");

        bVistaPrevia.setText("Vista Previa");
        bVistaPrevia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bVistaPreviaActionPerformed(evt);
            }
        });

        comboBoxClase.setMaximumRowCount(6);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(bBuscar)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel30)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textFieldDocumentoTitular, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bVistaPrevia)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(comboBoxClase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(49, 49, 49))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel30)
                    .addComponent(textFieldDocumentoTitular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(comboBoxClase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bVistaPrevia)
                    .addComponent(bBuscar))
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setPreferredSize(new java.awt.Dimension(400, 200));

        labelTipoDNI.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelTipoDNI.setText("DNI");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("APELLIDO");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("NOMBRE");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("FECHA DE NAC.");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("DOMICILIO");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel7.setText("OTORGAMIENTO");

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel8.setText("VENCIMIENTO");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel9.setText("CLASE");

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel10.setText("CATEGORIA");

        labelDNI.setText("-               ");

        labelApellido.setText("-");

        labelNombre.setText("-");

        labelFechaNacimiento.setText("-");

        labelDomicilio.setText("-");

        labelOtorgamiento.setText("-");

        labelVencimiento.setText("-");

        labelClase.setText("-");

        labelCategoria.setText("-");

        labelImagen.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelImagen.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        labelImagen.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(labelImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(labelImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel6.setText("Licencia de Conducir");

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jLabel11.setText("Ciudad de Santa Fe");

        jSeparator1.setForeground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addGap(126, 126, 126))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(100, 100, 100))))
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelVencimiento)
                            .addComponent(labelOtorgamiento))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel10)
                    .addComponent(labelTipoDNI)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(labelCategoria, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelClase, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelDomicilio, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelFechaNacimiento, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelNombre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelApellido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelDNI))
                .addGap(64, 64, 64))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labelTipoDNI)
                            .addComponent(labelDNI))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(labelApellido))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(labelNombre))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(labelFechaNacimiento))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(labelDomicilio)))
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelOtorgamiento)
                    .addComponent(jLabel7)
                    .addComponent(jLabel9)
                    .addComponent(labelClase))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(labelVencimiento)
                    .addComponent(jLabel10)
                    .addComponent(labelCategoria))
                .addGap(18, 18, 18))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel4.setPreferredSize(new java.awt.Dimension(400, 200));

        jLabel22.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel22.setText("OBSERVACIONES");

        jLabel24.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel24.setText("DONANTE");

        jLabel25.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel25.setText("GRUPO Y FACTOR");

        labelDonante.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelDonante.setText("-");

        labelGrupoSanguineo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelGrupoSanguineo.setText("-");

        labelObservaciones.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        labelObservaciones.setText("-");
        labelObservaciones.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        labelObservaciones.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        labelFactor.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelFactor.setText("-");

        labelCodigo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelCodigo.setText("CODIGO");
        labelCodigo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        labelImagenCodigo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelImagenCodigo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel12.setText("B.1 - AUTO, UTILITARIO, CAMIONETA Y CASA RODANTE MOTOR                       A.3 - MOTO Y TRICICLO DE 300CC");
        jLabel12.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabel12.setAutoscrolls(true);
        jLabel12.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jLabel12.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel22)
                        .addGap(35, 35, 35)
                        .addComponent(jLabel24)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelDonante)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel25)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelGrupoSanguineo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelFactor))
                    .addComponent(labelObservaciones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelImagenCodigo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(labelCodigo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(45, 45, 45))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel22)
                    .addComponent(jLabel24)
                    .addComponent(labelDonante)
                    .addComponent(jLabel25)
                    .addComponent(labelGrupoSanguineo)
                    .addComponent(labelFactor))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelObservaciones, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelImagenCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelCodigo)
                .addGap(19, 19, 19))
        );

        labelImagenCodigo.getAccessibleContext().setAccessibleDescription("");

        jLabel20.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel20.setText("Frente");

        jLabel23.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel23.setText("Dorso");

        bCancelar.setText("Cancelar");
        bCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelarActionPerformed(evt);
            }
        });

        bImprimir.setText("Imprimir");
        bImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bImprimirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(99, 99, 99)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel20)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, 0))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel23)
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGap(221, 221, 221)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(234, 234, 234))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(bImprimir)
                .addGap(55, 55, 55)
                .addComponent(bCancelar)
                .addGap(82, 82, 82))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel20)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addComponent(jLabel23)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bCancelar)
                    .addComponent(bImprimir))
                .addContainerGap())
        );

        jPanel1.getAccessibleContext().setAccessibleName("");
        jPanel4.getAccessibleContext().setAccessibleName("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelarActionPerformed
        // TODO add your handling code here:
        MenuOperador volver = new MenuOperador();
        volver.setVisible(true);
        dispose();
    }//GEN-LAST:event_bCancelarActionPerformed

    private void bImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bImprimirActionPerformed
        // TODO add your handling code here:   
        if (buscar==false)
            JOptionPane.showMessageDialog(null, "Aún no se buscó ninguna licencia valida para imprimir.");
        else 
        {
            Licencia licenciaSeleccionada = seleccionarLicencia(comboBoxClase.getSelectedItem().toString());
            fieldFiller(titularValidado,licenciaSeleccionada);
            creaReporte();
        }
    }//GEN-LAST:event_bImprimirActionPerformed

    private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBuscarActionPerformed
        // TODO add your handling code here:
        if(textFieldDocumentoTitular.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Ingrese un DNI válido."+textFieldDocumentoTitular.getText());
            buscar=false;
        } else{
            //Con el dni ingresado busca en la BD si existe el titular
            documentoTitular=Integer.parseInt(textFieldDocumentoTitular.getText());
            boolean existeTitular=BD.ABM_Titular.obtenerTitular(titularValidado, documentoTitular);
            
            //En caso de existir el titular, busca en la BD las licencias activas que posee
            if(existeTitular){
                BD.ABM_Licencia.obtenerLicencias(licenciasValidadas, documentoTitular);
                
                //Llena el ComboBox con las licencias obtenidas y activas
                int i=0;
                DefaultComboBoxModel modeloCombo = new DefaultComboBoxModel();
                do {
                    modeloCombo.addElement(licenciasValidadas[i].getTipoLicencia());
                    i++;
                } while ((i<6) && (licenciasValidadas[i].getTipoLicencia()!=null));
                comboBoxClase.setModel(modeloCombo);
                buscar=true;
            }
        }
    }//GEN-LAST:event_bBuscarActionPerformed

    private void textFieldDocumentoTitularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldDocumentoTitularActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textFieldDocumentoTitularActionPerformed

    private void bVistaPreviaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bVistaPreviaActionPerformed
        // TODO add your handling code here:
        if (buscar==false)
            JOptionPane.showMessageDialog(null, "Aún no se buscó ninguna licencia valida para ver.");
        else {
            Licencia licenciaSeleccionada = seleccionarLicencia(comboBoxClase.getSelectedItem().toString());
            fieldFiller(titularValidado,licenciaSeleccionada);
        }
    }//GEN-LAST:event_bVistaPreviaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ImprimirLicencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ImprimirLicencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ImprimirLicencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ImprimirLicencia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ImprimirLicencia().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBuscar;
    private javax.swing.JButton bCancelar;
    private javax.swing.JButton bImprimir;
    private javax.swing.JButton bVistaPrevia;
    private javax.swing.JComboBox comboBoxClase;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel30;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel labelApellido;
    private javax.swing.JLabel labelCategoria;
    private javax.swing.JLabel labelClase;
    private javax.swing.JLabel labelCodigo;
    private javax.swing.JLabel labelDNI;
    private javax.swing.JLabel labelDomicilio;
    private javax.swing.JLabel labelDonante;
    private javax.swing.JLabel labelFactor;
    private javax.swing.JLabel labelFechaNacimiento;
    private javax.swing.JLabel labelGrupoSanguineo;
    private javax.swing.JLabel labelImagen;
    private javax.swing.JLabel labelImagenCodigo;
    private javax.swing.JLabel labelNombre;
    private javax.swing.JLabel labelObservaciones;
    private javax.swing.JLabel labelOtorgamiento;
    private javax.swing.JLabel labelTipoDNI;
    private javax.swing.JLabel labelVencimiento;
    private javax.swing.JTextField textFieldDocumentoTitular;
    // End of variables declaration//GEN-END:variables
}
